'use strict';

const RouterService = require('./base');

class BannerService extends RouterService {
  constructor(ctx) {
    super(ctx, 'Collection');
  }
  getPageInclude() {
    return [
      {
        model: this.ctx.model.Product,
        as: 'product',
      }
    ]
  }

  async create( data) {
    const { user_id, product_id } = data;
    if (!user_id) {
      throw new Error('请登录');
    }
    const collection = await this.ctx.model.Collection.findOne({
      where: {
        user_id,
        product_id
      }
    });
    if (collection) {
      // 删除
      return await this.deleteById(collection.id)
    } else {
      return await super.create(data);
    }
  }

}

module.exports = BannerService;
